package com.kang.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import reactor.core.publisher.Mono;


/**
 * 自定义请求限制的类
 * <使用哪种配置 获取参数值就行>
 */
@Configuration
public class RequestLimitConfig {

    /**
     * 在注解增加BeanName配置：@Bean("ipKeyResolver")和@Primary,带@Primary注解的优先，表示是主Bean。消费者使用@Qualifier("name")消费Bean
     * @return
     */

    //针对某一个ip /loginService/doLogin   每个IP十秒内只能访问三次
    @Bean("ipKeyResolver")
    @Primary
    public KeyResolver ipKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getHeaders().getHost().getHostString());
    }


    //针对路径进行访问  /loginService/doLogin  这个路径十秒内只能被访问三次
    @Bean("apiKeyResolver")
    public KeyResolver apiKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getPath().value());
    }




}
